import {
  queryAllSystemData,
  queryAllGuestSystemData,
  queryAllSystemSys,
  queryAllGuestSystemSys
} from '@/api/portal/manager/analysis';
import { getToken } from '@/utils/auth';
import {
  queryAllServiceNoAuthByOrganization,
  queryAllServiceAuthByOrganization
} from '@/api/portal/manager/setting/index';
const pickerOptions = {
  shortcuts: [
    {
      text: '最近一周',
      onClick(picker) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
        picker.$emit('pick', [start, end]);
      }
    },
    {
      text: '最近一个月',
      onClick(picker) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        picker.$emit('pick', [start, end]);
      }
    },
    {
      text: '最近三个月',
      onClick(picker) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
        picker.$emit('pick', [start, end]);
      }
    }
  ]
};
export const template = {
  data() {
    return {
      setting: null,
      column: [],
      dataList: [],
      timer: null,
      cid: null,
      isRefresh: false,
      refreshTime: 30,
      isOpenEntity: false, // 是否打开详情页
      entityTemplateId: null, // 详情模版编号
      entityOpenType: null, // 详情页打开方式
      entityParams: [], // 查询详情参数
      entityUrl: '', // 详情地址
      active: null,
      activeStyle: {},
      customStyle: {},
      headerStyle: {},
      titleStyle: {},
      bodyStyle: {},
      borderStyle: {},
      borderColumn: {},
      customRight: {},
      titleFont: {},
      pagerSize: 0,
      pickerOptions: pickerOptions,
      form: {
        page: 1,
        size: 10,
        cid: null,
        TITLE: ''
      }
    };
  },
  methods: {
    initData() {
      if (getToken()) {
        queryAllSystemData({ cid: this.cid })
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            } else if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemData({ cid: this.cid })
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            } else if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      }
    },
    callBack() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank');
        }
      } else if (this.temp.dkfs === 1) {
        this.$emit('fetch-data', this.temp);
      } else {
        console.log('');
      }
    },
    queryEntity() {
      this.form.page = 1;
      this.initData();
    },
    handleCurrentChange(obj) {
      this.form.page = obj;
      this.initData();
    },
    handleSizeChange(obj) {
      this.form.size = obj;
      this.form.page = 1;
      this.initData();
    },
    closeAlert(index) {
      this.active = null;
    },
    openAlert(index) {
      this.active = index;
    },
    openMore() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank');
        }
      } else if (this.temp.dkfs === 1) {
        this.$store
          .dispatch('GetThemes')
          .then(res => {
            return true;
          })
          .catch(error => {
            console.log(error);
          });
        this.$emit('fetch-data', this.temp);
      } else {
        console.log('');
      }
    }
  }
};

/**
 * 更多页面
 * @type {{data(): {form: {size: number, TITLE: string, page: number, cid: null}, customStyle: {}, activeStyle: {}, dataList: [], pickerOptions: {shortcuts: [{onClick(*): void, text: string},{onClick(*): void, text: string},{onClick(*): void, text: string}]}, column: {}, bodyStyle: {}, active: null, titleStyle: {}, setting: {}, pagerSize: number}, methods: {closeAlert(*): void, openEntity(*): void, initData(): void, openAlert(*): void, queryEntity(): void, handleSizeChange(*): void, handleCurrentChange(*): void}}}
 */
export const more = {
  data() {
    return {
      column: {},
      setting: {},
      dataList: [],
      pagerSize: 0,
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      pickerOptions: pickerOptions,
      form: {
        page: 1,
        size: 2000, // 控制更多页面显示数量2
        cid: null,
        TITLE: ''
      },
      activeStyle: {},
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      active: null,
      focus: false,
      isShow: false
    };
  },
  methods: {
    userFocusin() {
      this.focus = true;
    },
    search() {
      this.initData();
    },
    queryEntity() {
      this.form.page = 1;
      this.initData();
    },
    handleCurrentChange(obj) {
      this.form.page = obj;
      this.initData();
    },
    handleSizeChange(obj) {
      this.form.size = obj;
      this.form.page = 1;
      this.initData();
    },
    closeAlert(index) {
      this.active = null;
    },
    openAlert(index) {
      this.active = index;
    },
    initData() {
      if (getToken()) {
        queryAllSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.pager && data.pager !== null) {
              this.pagerSize = data.pager.totalElements;
            }
            if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
            } else if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemSys(this.form)
          .then(res => {
            const { data } = res;
            if (undefined !== data.pager && data.pager !== null) {
              this.pagerSize = data.pager.totalElements;
            }
            if (undefined !== data.data && data.data !== null) {
              this.dataList = data.data;
            } else if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
            }
          })
          .catch(() => {
            this.dataList = [];
          });
      }
    },
    queryBack(data) {
      if (undefined !== data) {
        const obj = this.column.query.filter(item => {
          if (data.id === item.sjx) {
            return item;
          }
        });
        this.form[obj[0].column] = data.value;
        console.log(this.form);
        this.initData();
      }
    },
    userFocusout() {
      this.focus = false;
    }
  }
};

export const service = {
  data() {
    return {
      dataList: [],
      column: [],
      queryList: [],
      baseURL: process.env.VUE_APP_BASE_API.substring(
        0,
        process.env.VUE_APP_BASE_API.length - 1
      ),
      query: {
        blurry: ''
      },
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      borderStyle: {},
      setting: []
    };
  },
  methods: {
    queryObject() {
      this.initData();
    },
    initData() {
      if (getToken()) {
        queryAllServiceAuthByOrganization(this.query)
          .then(res => {
            const { data } = res;
            this.dataList = data.data;
          })
          .catch(() => {
            this.dataList = [];
          });
      } else {
        queryAllServiceNoAuthByOrganization(this.query)
          .then(res => {
            const { data } = res;
            this.dataList = data.data;
          })
          .catch(() => {
            this.dataList = [];
          });
      }
    },
    queryClick(obj) {
      this.query[obj.code] = obj.value;
      this.initData();
    }
  }
};

export const openEntity = {
  methods: {
    openEntity(data) {
      // 打开详情页
      if (this.setting.isOpenEntity) {
        // 允许打开详情页
        if (this.setting.entityOpenType === 1) {
          // 地址参数的方式打开
          window.open(data[this.setting.entityUrl], '_blank');
        }
        if (this.setting.entityOpenType === 2) {
          // 地址传参查询
          if (
            undefined !== this.setting.params &&
            this.setting.params !== null &&
            this.setting.params.length > 0
          ) {
            let url = this.setting.entityUrl;
            this.setting.params.forEach(item => {
              url = url.replace(
                new RegExp(
                  '\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'
                ),
                data[item.column]
              );
            });
            window.open(url, '_blank');
          }
        }
        if (this.setting.entityOpenType === 3) {
          // 采用自定义模版的方式打开
          const params = [];
          if (
            undefined !== this.setting.entityParams &&
            this.setting.entityParams !== null &&
            this.setting.entityParams.length > 0
          ) {
            this.setting.entityParams.forEach(item => {
              const obj = { code: item.code, value: data[item.value] };
              params.push(obj);
            });
          }
          const object = {
            params: params,
            setting: this.temp,
            entityTemp: this.setting.entityTemplateId,
            temp: this.setting.entityTemp
          };
          this.$emit('fetch-entity', object);
        }
      }
    },
    // 点击统计项，打开类似更多模版
    openVisitingCard(data) {
      if (data.isOpenMore) {
        const entity = {
          id: data.temp,
          name: this.temp.lmmc, // 栏目名称
          dkfs: data.openType, // 打开方式
          url: data.openUrl, // 打开地址
          parentId: this.temp.column.menu.id, // 父节点编号
          parentName: this.temp.column.menu.name // 父节点名称
        };
        this.$bus.emit('openMore', entity);
      }
    }
  }
};
